home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 February / OpenLinux 2.3 CD.iso / live / sbin / hwprobe < prev    next >
Encoding:
Text File  |  1999-08-10  |  3.6 KB  |  160 lines

  1. #!/usr/bin/perl
  2.  
  3. $etc = "";            # debug in current dir
  4. $etc = "/etc/";
  5.  
  6. $outfname = $etc . "modules/default";
  7. $tmpfname = "$outfname.new";
  8.  
  9. $optfn = $etc . "modules.conf";
  10. $noptfn = "$optfn.new";
  11.  
  12. $reuse = 1;
  13. open(DEFIN, "<$outfname") || die "no modules input file";
  14. open(CFG, "<$ARGV[0]") || open (CFG,"</etc/hwprobe.config") || die "no config file: $!\n";
  15.  
  16. open(DEFOUT, ">$tmpfname");
  17. open(OPTOUT, ">$noptfn");
  18. $mymodlist = "(?: no_module ";
  19.  
  20. while(<DEFIN>) {
  21.   /\@\@/ && last;
  22.   print DEFOUT $_;
  23. }
  24.  
  25. if ($reuse) {
  26.   while (<DEFIN>) {
  27.     /\@\@/ && last;
  28.   }
  29. }
  30.  
  31. print DEFOUT "#@@ Automatically generated driver list -- don't edit up to the next @@ - line\n";
  32.  
  33. $thisdrv = "<none yet specified>";
  34. while(<CFG>)
  35. {
  36.     m/^\s*#/ && next;
  37.     m/^\s*$/ && next;
  38.     $line = $_;
  39.     chop $line if (substr($line, -1, 1) eq "\n");
  40.     while(substr($line, -1, 1) eq "\\" && !eof(CFG))
  41.     {
  42.     chop $line;
  43.     $line .= <CFG>;
  44.     chop $line if (substr($line, -1, 1) eq "\n");
  45.     }
  46.     $line =~ s/\s+#.*$//;
  47.     @line = split /\s+/, $line;
  48. #    print "(" . join(" ", @line) . ")\n";
  49.  
  50.     if ($line[0] =~ m/^driver$/i) { shift @line; $thisdrv = join(" ", @line); }
  51.     $map{uc $line[0]} = $thisdrv;
  52. }
  53. close CFG;
  54.  
  55. if (open(PCI, "</proc/bus/pci/devices"))
  56. {
  57.     while(<PCI>)
  58.     {
  59.     chop; split;
  60.     $match = $map{uc $_[1]};
  61.     if ($match && !defined($handled{$match})) {
  62.       $handled{$match}=1;
  63.       print DEFOUT "$match\n";
  64.     }
  65.     }
  66.     close PCI;
  67. }
  68.  
  69. sub modflush {
  70.   foreach $key (keys %modules) {
  71.     print DEFOUT "$key\n";
  72. #    print OPTOUT "options $key" . $modules{$key} . "\n";
  73.     $opts .= "options $key" . $modules{$key} . "\n";
  74.     $mymodlist .= "|$key";
  75.   }
  76.   undef %modules;
  77. }
  78.  
  79. if (open(PNP, "</proc/bus/isapnp/devices"))
  80. {
  81.   $boardno = 1;
  82.   while(<PNP>)
  83.     {
  84.       /^00/ && next;        # omit board IDs unless it shows we need them
  85.       if ( ! /^$boardno/ ) {$boardno = substr($_, 0, 1); modflush(); };
  86.       chop; @P = split;
  87.       $match = $map{uc $P[2]} unless $match = $map{uc $P[1]} ;
  88.       
  89.       if ($match)
  90.     {
  91.       @match = split " ", $match;
  92.       $modname =  $match[0];
  93.       shift @match;
  94.       shift @P; shift @P; shift @P;    # drop CSN/LD and Device / Compat ID
  95.       
  96.       while (@match > 0 && @P > 0)
  97.         {
  98.           $param = shift @match;
  99.           $value = shift @P;
  100.           
  101.           # detect PnP values for "unassigned" and drop those params
  102.           if ($value =~ /I0|D4|^0x0+$/) { next; } # unassigned: skip
  103.           if ($param =~ /^!/) { next; } # skip on config file request
  104.           
  105.           $value =~ s/^I|^D/0x/;
  106.  
  107.           $or_val = 0;
  108.           if ($param =~ s/\|([0-9a-fA-FXx]+)$//) {
  109.         $or_val = $1;
  110.           }
  111.  
  112.           $value = oct($value) if $value =~ /^0/;
  113.           $or_val = oct($or_val) if $or_val =~ /^0/;
  114.           $value = 0+$value | 0+$or_val;
  115.           $value = sprintf("0x%x", $value) if $value >= 16;
  116.  
  117.           if ($param =~ s,/, ,) {
  118.         ($tmodname, $param) = split(" ", $param);
  119.         $param =~ s/=(I[01]|D[01]|P[0-7]|M[0-3])/=$value/i;
  120. #        print"$tmodname $param\n";
  121.         $modules{$tmodname} .= " $param";
  122.           } else {
  123.         $param =~ s/=.*$//; # XXX make sure this corresponds with
  124.                     # type & num of the value above !
  125. #        $modstr .= " $param=$value";
  126.         $modules{$modname} .= " $param=$value";
  127.         
  128.           }
  129.         }
  130.     }
  131.     }
  132.   close PNP;
  133.   modflush();
  134. }
  135.  
  136. print DEFOUT "#@@ End of automatically generated driver list\n";
  137.  
  138. while(<DEFIN>) {
  139.   print DEFOUT $_;
  140. }
  141. close(DEFIN);
  142. close(DEFOUT);
  143.  
  144. unlink("$outfname");
  145. link("$tmpfname", "$outfname") && unlink("$tmpfname") == 1;
  146.  
  147. $mymodlist .= ")";
  148.  
  149. open(MC, "<$optfn");
  150.  
  151. while(<MC>) {
  152.   /\s*options\s+$mymodlist\s/ || print OPTOUT $_;
  153. }
  154.  
  155. print OPTOUT $opts;
  156. close(OPTOUT);
  157.  
  158. unlink("$optfn");
  159. link("$noptfn", "$optfn") && unlink("$noptfn");
  160.